package com.hup.timer.view.dialog;

import com.hup.utils.commons.exception.AppCommonException;
import com.hup.utils.commons.exception.AppExceptionHandler;
import com.hup.utils.commons.exception.ErrInfo;
import com.hup.utils.commons.hardware.SystemUtil;
import com.hup.utils.commons.thread.TimeCatStarter;
import com.hup.utils.commons.thread.TimeCatStarter.StarterHandler;
import com.hup.utils.io.batch.BatchUtil;
import com.hup.utils.io.batch.SimpleBatchResp;
import com.hup.utils.io.batch.function.ChangeTime;
import com.hup.utils.swing.customView.ViewText;
import com.hup.utils.swing.frame.BaseDialog;
import com.hup.utils.swing.frame.SingletonWindowHelper;
import lombok.extern.log4j.Log4j2;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;

@Log4j2
public class SyncTimeDialog extends BaseDialog {
    private final static SimpleDateFormat DF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    private final TimeCatStarter timeCat;
    private JPanel contentPane;
    private JButton btnSync;
    public ViewText labNetTime;
    public ViewText labLocalTime;
    public ViewText labErr;

    public SyncTimeDialog() {
        super("同步网络时间");
        setContentPane(contentPane);
        setModal(false);
        setEscapeCancelable(true);
        getRootPane().setDefaultButton(btnSync);
        labErr.setVisible(false);
        if (!BatchUtil.isAdmin()) {
            labErr.setText("无管理员权限");
            labErr.setVisible(true);
            btnSync.setEnabled(false);
        }

        btnSync.addActionListener(event -> doInBackground("同步时间", () -> {
            try {
                SimpleBatchResp resp = BatchUtil.builder().exec(new ChangeTime(SystemUtil.getNetTime(true)));
                if (!resp.success) throw new AppCommonException(resp.errMsg);
            } catch (Exception e) {
                onExecuteException("同步时间", e);
            }
        }));

        timeCat = new TimeCatStarter(false);
        timeCat.start(this, new StarterHandler() {
            @Override
            public void run(TimeCatStarter starter) {
                starter.perSecond(() -> {
                    labLocalTime.setText(DF.format(new Date()));
                    try {
                        labNetTime.setText(DF.format(SystemUtil.getNetTime(true)));
                    } catch (Exception e) {
                        onExecuteException("刷新网络时间", e);
                    }
                });
            }

            @Override
            public void onException(Exception e) {
                //不会有异常
            }
        });
    }

    @Override
    public void onExecuteException(String desc, Throwable e) {
        labErr.setVisible(true);
        ErrInfo errInfo = AppExceptionHandler.handle(desc + "错误:", e);
        labErr.setText(errInfo.getErrMsg());
        pack();
    }

    @Override
    public void onHidden(boolean dispose) {
        timeCat.doCancel();
    }

    public static void doShow() {
        SyncTimeDialog dialog = SingletonWindowHelper.getWindow(SyncTimeDialog.class);
        if (dialog == null) {
            dialog = new SyncTimeDialog();
            dialog.pack();
            dialog.setLocationRelativeTo(null);
        }
        dialog.setAlwaysOnTop(true);
        dialog.setVisible(true);
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        contentPane = new JPanel();
        contentPane.setLayout(new GridBagLayout());
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridBagLayout());
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(10, 10, 10, 10);
        contentPane.add(panel1, gbc);
        btnSync = new JButton();
        btnSync.setText("同步");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.gridheight = 2;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(0, 10, 0, 0);
        panel1.add(btnSync, gbc);
        final JLabel label1 = new JLabel();
        label1.setText("网络时间: ");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        panel1.add(label1, gbc);
        final JLabel label2 = new JLabel();
        label2.setText("本地时间: ");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel1.add(label2, gbc);
        labNetTime = new ViewText();
        labNetTime.setText("yyyy-MM-dd HH:mm:ss:SSS");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        panel1.add(labNetTime, gbc);
        labLocalTime = new ViewText();
        labLocalTime.setText("yyyy-MM-dd HH:mm:ss:SSS");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel1.add(labLocalTime, gbc);
        final JPanel panel2 = new JPanel();
        panel2.setLayout(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 3;
        gbc.fill = GridBagConstraints.BOTH;
        panel1.add(panel2, gbc);
        labErr = new ViewText();
        labErr.setText("异常文本");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        panel2.add(labErr, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() { return contentPane; }
}
